package com.sky.web.app;

import com.sky.context.ThreadLocalUtil;
import com.sky.entity.AddressBook;
import com.sky.entity.User;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
@Autowired
    private AddressBookService addressBookService;


    /**
     * 个人中心里的地址管理（查询登录人地址管理）
     * @return
     */
    @GetMapping("list")
    public Result list(){
        List<AddressBook> list=addressBookService.list();
        return Result.success(list);
}

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook) {
        //调用service
        addressBookService.add(addressBook);
        return Result.success();
}


    /**
     * 设置默认收货地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook){
        //调用service
    addressBookService.setDefaultAddress(addressBook);
    return Result.success();
}

    /**
     * 查询默认收货地址
     * @return
     */
    @GetMapping("/default")
    public Result getDefault(){
        //调用service方法
        AddressBook addressBook=addressBookService.getDefault();
        return Result.success(addressBook);
}

    /**
     * 修改地址簿
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result edit(@PathVariable Long id){
        //调用service根据id查询
        AddressBook addressBooks=addressBookService.getById(id);
        return Result.success(addressBooks);
}

    /**
     * 保存地址
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
         addressBookService.updateById(addressBook);
        return Result.success();
}


}
